package com.dongdongmovie.controller;

import com.dongdongmovie.pojo.PageResult;
import com.dongdongmovie.pojo.TMovie;
import com.dongdongmovie.service.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("TMovie")
public class TMovie_Controller {

    //注入service
    @DubboReference
    private TMovie_Service tMovie_service;
    @DubboReference
    private TYears_Service tYears_service;
    @DubboReference
    private TType_Service tType_service;
    @DubboReference
    private TArea_Service tArea_service;
    @DubboReference
    private TCharnnel_Service tCharnnel_service;
    @DubboReference
    private TTag_Service tTag_service;

    //跳转到show
    @RequestMapping("tzshow")
    public String tzshow(Model model) {
        model.addAttribute("tYears", tYears_service.show());
        model.addAttribute("tType", tType_service.show());
        model.addAttribute("tArea", tArea_service.show());
        return "TMovie/show";
    }

    //跳转到add
    @RequestMapping("tzadd")
    public String tzadd(Model model) {
        model.addAttribute("tYears", tYears_service.show());
        model.addAttribute("tType", tType_service.show());
        model.addAttribute("tArea", tArea_service.show());
        model.addAttribute("tCharnnel", tCharnnel_service.show());
        model.addAttribute("tTag", tTag_service.show());
        return "TMovie/add";
    }

    //跳转到update
    @RequestMapping("tzupdate")
    public String tzupdate(Integer id, Model model) {
        model.addAttribute("tYears", tYears_service.show());
        model.addAttribute("tType", tType_service.show());
        model.addAttribute("tArea", tArea_service.show());
        model.addAttribute("tCharnnel", tCharnnel_service.show());
        model.addAttribute("tTag", tTag_service.show());
        TMovie tCharnnel = tMovie_service.showbyId(id);
        model.addAttribute("TMovie", tCharnnel);
        return "TMovie/update";
    }

    //查询数据
    @ResponseBody
    @RequestMapping("show")
    public PageResult<TMovie> show(TMovie tMovie, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        PageResult<TMovie> show = tMovie_service.show(tMovie, pageNum, pageSize);
        request.getSession().setAttribute("poiout", show);
        return show;
    }

    //添加数据
    @ResponseBody
    @RequestMapping("add")
    public int add(TMovie tCharnnel, HttpServletRequest request) {
        return tMovie_service.add(tCharnnel);
    }

    //修改数据
    @ResponseBody
    @RequestMapping("update")
    public int update(TMovie tCharnnel) {
        return tMovie_service.update(tCharnnel);
    }

    //删除数据
    @ResponseBody
    @RequestMapping("delete")
    public int delete(Integer id) {
        return tMovie_service.delete(id);
    }

    //删除全部
    @ResponseBody
    @RequestMapping("deleteall")
    public int deleteall(@RequestParam(value = "rids[]") Integer[] rids) {
        return tMovie_service.deleteall(rids);
    }

    //上架
    @ResponseBody
    @RequestMapping("putaway")
    public int putaway(Integer rid) {
        return tMovie_service.updateputaway(rid);
    }

    //下架
    @ResponseBody
    @RequestMapping("soldout")
    public int soldout(Integer rid) {
        return tMovie_service.updatesoldout(rid);
    }

    //审核通过
    @ResponseBody
    @RequestMapping("tg")
    public int tg(Integer rid) {
        return tMovie_service.updatetg(rid);
    }

    //审核不通过
    @ResponseBody
    @RequestMapping("btg")
    public int btg(Integer rid) {
        return tMovie_service.updatebtg(rid);
    }

    //导出数据
    @ResponseBody
    @RequestMapping("poiout")
    public int poiout(HttpServletRequest request) {
        PageResult<TMovie> pageResult = (PageResult<TMovie>) request.getSession().getAttribute("poiout");
        List<TMovie> list = pageResult.getRows();
        //从项目 读取后写入 xls xlsx 表格
        //创建workbook
        Workbook workbook = new HSSFWorkbook();
        //根据workbook创建一个sheet表
        Sheet sheet1 = workbook.createSheet("sheet1");
        //创建表头
        org.apache.poi.ss.usermodel.Row row = sheet1.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("影视名称");
        row.createCell(2).setCellValue("影视图片");
        row.createCell(3).setCellValue("简介");
        row.createCell(4).setCellValue("演员");
        row.createCell(5).setCellValue("类型");
        row.createCell(6).setCellValue("产地");
        row.createCell(7).setCellValue("上映年");
        row.createCell(8).setCellValue("评分");
        row.createCell(9).setCellValue("上映时间");
        row.createCell(10).setCellValue("上下架");
        row.createCell(11).setCellValue("状态");
        row.createCell(12).setCellValue("播放来源");
        row.createCell(13).setCellValue("标签");
        row.createCell(14).setCellValue("所属栏位");
        //5 将查询的结果放入表格中
        for (int i = 0; i < list.size(); i++) {
            TMovie tMovie = list.get(i);
            Row row1 = sheet1.createRow(i + 1);
            row1.createCell(0).setCellValue(tMovie.getId());
            row1.createCell(1).setCellValue(tMovie.getMovieName());
            row1.createCell(2).setCellValue(tMovie.getMovieImage());
            row1.createCell(3).setCellValue(tMovie.getIntroduction());
            row1.createCell(4).setCellValue(tMovie.getActor());
            row1.createCell(5).setCellValue(tMovie.gettName());
            row1.createCell(6).setCellValue(tMovie.getAreaName());
            row1.createCell(7).setCellValue(tMovie.getyName());
            row1.createCell(8).setCellValue(tMovie.getScore());
            row1.createCell(9).setCellValue(tMovie.getUpdateTime());
            if (tMovie.getUp()==2){
                row1.createCell(10).setCellValue("已下架");
            }else {
                row1.createCell(10).setCellValue("已上架");
            }
            if(tMovie.getExamine()==1){
                row1.createCell(11).setCellValue("审核通过");
            } else if(tMovie.getExamine()==2){
                row1.createCell(11).setCellValue("审核失败");
            }else {
                row1.createCell(11).setCellValue("等待审核");
            }
            row1.createCell(12).setCellValue(tMovie.getPlayType());
            row1.createCell(13).setCellValue(tMovie.getTagName());
            row1.createCell(14).setCellValue(tMovie.getcName());
        }
        try {
            FileOutputStream fis = new FileOutputStream("E:\\" + UUID.randomUUID() + ".xls");
            workbook.write(fis);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

}
